(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.passwordPolicy.createController', createController);
	createController.$inject = [ '$scope', '$q', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'passwordPolicyService', 'storageService' ];
	function createController($scope, $q, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, passwordPolicyService, storageService) {
		var ctrl = this, passwordPolicyInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.passwordPolicy = {};
		
		ctrl.init = function() {
			ctrl.siteCode = storageService.getUserInfo().siteCode;
			passwordPolicyService.getValidationDatas().then(function(response){
				return $q(function(resolved, rejected){
				       ctrl.isEdit = response ? response.isEdit : {};
				       resolved();
				});
			}).then(function(){
				authService.create({
					siteCode : passwordPolicyInfo.siteCode
				}).then(function(response) {
					ctrl.passwordPolicy = response.data || passwordPolicyInfo;
					passwordPolicyService.initDefault().then(function(result){
						ctrl.passwordPolicy.siteCode = ctrl.siteCode;
						ctrl.passwordPolicy.enforcePolicyFlag = result.enforcePolicyFlag;
						ctrl.passwordPolicy.lenFlag = result.lenFlag;
						ctrl.passwordPolicy.lenMin = result.lenMin;
						ctrl.passwordPolicy.lenMax = result.lenMax;
						ctrl.passwordPolicy.pwdHistoryLen = result.pwdHistoryLen;
						ctrl.passwordPolicy.specialCharsNum = result.specialCharsNum;
						ctrl.passwordPolicy.specialChars = result.specialChars;
						ctrl.passwordPolicy.expiryDays = result.expiryDays;
						ctrl.passwordPolicy.numLogonTry = result.numLogonTry;
						ctrl.passwordPolicy.autoUnlockPeriod = result.autoUnlockPeriod;
						ctrl.passwordPolicy.autoUnlockFlag = result.autoUnlockFlag;
						ctrl.passwordPolicy.mixCasesFlag = result.mixCasesFlag;
						ctrl.passwordPolicy.inactivitySiteuserFlag = result.inactivitySiteuserFlag;
					});
				});
			});
		};
        
		ctrl.createSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.passwordPolicy.taskId) {
				return authService.createContinueSave(ctrl.passwordPolicy).then(saveSuccessful, saveFailed);
			}  else {
				return authService.createSave(ctrl.passwordPolicy).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.passwordPolicy.taskId) {
				return authService.createContinueSubmit(ctrl.passwordPolicy).then(submitSuccessful, submitFailed);
			}  else {
				return authService.createSubmit(ctrl.passwordPolicy).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.passwordPolicy.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.passwordpolicy.list',
					'state' : 'cda.passwordPolicy.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			
			if (response.data && response.data.model) {
				ctrl.passwordPolicy = response.data.model;
			}
			
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			};
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		};
	};
})(angular);